{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-26T12:31:51.243355Z",
     "start_time": "2025-02-26T12:31:51.074012Z"
    }
   },
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np\n"
   ],
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 创建一维数组",
   "id": "64cd3905a50cc404"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T01:49:39.856924Z",
     "start_time": "2025-02-26T01:49:39.848985Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list1 = [1,2,3,4]\n",
    "oneArray = np.array(list1) #创建一维数组\n",
    "print(type(oneArray))  #打印对象类型 <class 'numpy.ndarray'>\n",
    "print(list1) #打印原始列表 [1, 2, 3, 4]\n",
    "print(oneArray) #打印一维数组 [1 2 3 4]\n",
    "\n",
    "# 创建数组的多种形式\n",
    "# 1. 直接传入列表的方式\n",
    "t1 = np.array([1,2,3]) #array([1, 2, 3])函数创建数组\n",
    "print(t1) #打印数组[1 2 3]\n",
    "print(type(t1))\n",
    "\n",
    "# 2. 传入range生成序列\n",
    "t2 = np.array(range(10)) #array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
    "print(t2) #打印数组[0 1 2 3 4 5 6 7 8 9]\n",
    "print(type(t2))\n",
    "\n",
    "# 3. 使用numpy自带的np.arange()生成数组\n",
    "t3 = np.arange(0,10,2) #array([0, 2, 4, 6, 8]),从0开始，到10结束，步长为2\n",
    "print(t3) #打印数组[0 2 4 6 8]\n",
    "print(type(t3))"
   ],
   "id": "61ddce4eca9cac8f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1, 2, 3, 4]\n",
      "[1 2 3 4]\n",
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n",
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# ndarray对象\n",
    "# 对比列表和ndarray的运算效率"
   ],
   "id": "432a1cbd759e32b"
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "code",
   "source": [
    "a=[]\n",
    "for _ in range(10**8): #生成100000000的序列\n",
    "   a.append(random.random()) #生成随机数\n",
    "t1 = time.time() #记录开始时间\n",
    "sum1 = sum(a) #列表求和\n",
    "t2 = time.time() #记录结束时间\n",
    "\n",
    "b = np.array(a) #转换为ndarray,ndarray的运算效率更高\n",
    "t4 = time.time() #记录结束时间\n",
    "sum3 = np.sum(b) #ndarray求和\n",
    "t5 = time.time() #记录结束时间\n",
    "print(t2-t1, t5-t4) #打印列表和ndarray的运算时间差"
   ],
   "id": "adda4220edce978e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.2创建二维数组,及常见属性",
   "id": "d41f335316b4ca31"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T01:49:43.544682Z",
     "start_time": "2025-02-26T01:49:43.538283Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list2 = [[1,2],[3,4],[5,6]]\n",
    "twoArray = np.array(list2)\n",
    "# 获取数组的维度\n",
    "print(twoArray.ndim)\n",
    "# 形状（行，列）\n",
    "print(twoArray.shape)\n",
    "# 元素个数\n",
    "print(twoArray.size)"
   ],
   "id": "647c40a2ad9dfeff",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.3 调整数组形状",
   "id": "7b7cb12ac2074d60"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T12:52:59.509397Z",
     "start_time": "2025-02-25T12:52:59.496968Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four = np.array([[1,2,3],[4,5,6]])  #注意这里是array([])\n",
    "\n",
    "# 修改的是原有的\n",
    "print(id(four))\n",
    "four.shape = (3,2) #shape用于修改形状，将2行3列的数组变为3行2列的数组\n",
    "print('-'*50)\n",
    "print(f'four={four},id(four)={id(four)}')\n",
    "print('-'*50)\n",
    "# 返回一个新的数组\n",
    "four1 = four.reshape(3,2) #reshape用于返回一个新的数组\n",
    "print(four1)\n",
    "print('-'*50)\n",
    "print(id(four1))"
   ],
   "id": "efc694014c600839",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2862953456496\n",
      "--------------------------------------------------\n",
      "--------------------------------------------------\n",
      "four=[[1 2]\n",
      " [3 4]\n",
      " [5 6]],id(four)=2862953456496\n",
      "--------------------------------------------------\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "--------------------------------------------------\n",
      "2862953463408\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 4.5数组变列表",
   "id": "55d11fc5110f42ad"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:45:04.476424Z",
     "start_time": "2025-02-25T07:45:04.470973Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([1,2,3,4,5])\n",
    "liat1 = a.tolist()\n",
    "print(liat1)"
   ],
   "id": "7573870e1a685f29",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 5数据类型\n",
   "id": "38109d36e6439b5f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T12:57:37.787528Z",
     "start_time": "2025-02-25T12:57:37.750233Z"
    }
   },
   "cell_type": "code",
   "source": [
    "f = np.array([1,2,3,4,5],dtype=np.int16)\n",
    "print(f.itemsize) # 1 np.int8(一个字节)\n",
    "# 获取数据类型\n",
    "print(f.dtype)\n",
    "\n",
    "# 调整数据类型\n",
    "f1 = f.astype(np.int64) # 调整为int64类型\n",
    "print(f1.dtype)\n",
    "\n",
    "# 拓展随机生成小数\n",
    "# 使用python语法，保留两位\n",
    "print(round(random.random(),2))\n",
    "\n",
    "arr = np.array([random.random() for i in range(10)],dtype=np.float16) #生成10个随机小数，精度为0.01\n",
    "# 取小数点后两位\n",
    "print(np.round(arr,2))\n",
    "print(arr.dtype)"
   ],
   "id": "4a764290046479e1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n",
      "int64\n",
      "0.99\n",
      "[0.78 0.98 0.13 0.99 0.09 0.68 0.56 0.55 0.39 0.84]\n",
      "float16\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 6.1 数组与数的计算\n",
   "id": "eea1c797e70c9e9c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:54:09.407744Z",
     "start_time": "2025-02-25T07:54:09.389570Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(24).reshape((6,4))\n",
    "print(a+1) # 数组+1\n",
    "print('-'*20)\n",
    "print(a*2) # 数组*2\n",
    "print('-'*20)\n",
    "print(a/2) # 数组/2"
   ],
   "id": "c03fbaf4ebbe15c9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]\n",
      " [13 14 15 16]\n",
      " [17 18 19 20]\n",
      " [21 22 23 24]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 6.2 数组与数组的计算",
   "id": "a57ab544e41c01fc"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T07:58:33.657283Z",
     "start_time": "2025-02-25T07:58:33.647250Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的形状不一样不能计算，数组的尺寸不一样，某一数组的某一个维度的长度为1，可以计算\n",
    "a = np.arange(24).reshape((4,6))\n",
    "b = np.arange(0,6).reshape((1,6))\n",
    "print(a)\n",
    "print('-'*20)\n",
    "print(b)\n",
    "print('-'*20)\n",
    "print(a+b)"
   ],
   "id": "f3067530e97c4602",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------\n",
      "[[0 1 2 3 4 5]]\n",
      "--------------------\n",
      "[[ 0  2  4  6  8 10]\n",
      " [ 6  8 10 12 14 16]\n",
      " [12 14 16 18 20 22]\n",
      " [18 20 22 24 26 28]]\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 6.3 练习轴",
   "id": "28fb1726eee244ed"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-25T08:07:41.478624Z",
     "start_time": "2025-02-25T08:07:41.470151Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "# 按照哪个轴求和，哪个轴就消失\n",
    "print(np.sum(a,axis=0))\n",
    "print('-'*20)\n",
    "print(np.sum(a,axis=1))\n",
    "print('-'*20)\n",
    "print(np.sum(a)) #默认是求所有元素的和"
   ],
   "id": "d1650e9e50eb46f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "--------------------\n",
      "[ 6 15]\n",
      "--------------------\n",
      "21\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 7索引,切片",
   "id": "6dd495bafcf652e7"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "a = np.arange(10)\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片操作print(a[2:7:2])# 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "\n",
    "# 如果只放置一个参数，如 [2]，将返回与该索引相对应的单个元素\n",
    "print(a[2:4])\n",
    "\n",
    "# 如果为 [2:]，表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2::2])"
   ],
   "id": "49759165b6820004"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:57:34.325203Z",
     "start_time": "2025-02-26T08:57:34.201417Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape(4,6)\n",
    "print(t1)\n",
    "print('*'*20)\n",
    "print(t1[1]) # 取一行(一行代表是一条数据，索引也是从0开始的) print(t1[1,:]) # 取一行\n",
    "print('*'*20)\n",
    "print(t1[1:])# 取连续的多行\n",
    "print('*'*20)\n",
    "print(t1[1:3,:])# 取连续的多行\n",
    "print('*'*20)"
   ],
   "id": "24a8adfcbd686f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 6  7  8  9 10 11]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "********************\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "print(t1[[0,2,3]])# 取不连续的多行\n",
    "print('*'*20)\n",
    "print(t1[[0,2,3],:])# 取不连续的多行\n",
    "print('*'*20)\n",
    "print(t1[:,1])# 取一列\n",
    "print('*'*20)\n",
    "print(t1[:,1:])# 连续的多列\n",
    "print('*'*20)\n",
    "print(t1[:,[0,2,3]])# 取不连续的多列\n",
    "print('*'*20)\n",
    "print(t1[2,3])# # 取某一个值,三行四列\n",
    "print('*'*20)\n",
    "print(t1[2:4,3:5])# 取某一块区域\n",
    "print('*'*20)\n",
    "print(t1[[1,2],[0,2]])"
   ],
   "id": "cbf4271cace039db"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 8 数组中的值修改",
   "id": "5fce0a454297ea4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:58:47.884255Z",
     "start_time": "2025-02-26T08:58:47.866147Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# # 修改某一行的值\n",
    "# t[1, :] = 0\n",
    "#\n",
    "# # 修改某一列的值\n",
    "# t[:, 1] = 0\n",
    "#\n",
    "# # 修改连续多行\n",
    "# t[1:3, :] = 0\n",
    "#\n",
    "# # 修改连续多列\n",
    "# t[:, 1:4] = 0\n",
    "#\n",
    "# # 修改多行多列，取第二行到第四行，第三列到第五列\n",
    "# t[1:4,2:5]=0\n",
    "#\n",
    "# # 修改多个不相邻的点\n",
    "# t[[0,1],[2,3]]=0\n",
    "# print(t)\n",
    "# 可以根据条件修改，比如讲小于10的值改掉\n",
    "# t[t < 10] = 0\n",
    "# print(t)\n",
    "# 使用逻辑判断\n",
    "# np.logical_and   & # np.logical_or  |\n",
    "# np.logical_not      ~\n",
    "# t[(t > 2) & (t < 6)] = 0  # 与\n",
    "# t[(t < 2) | (t > 6)] = 0  # 或\n",
    "t = np.arange(24).reshape(4, 6)\n",
    "t[~(t > 6)] = 0  # 大于6的改为0\n",
    "print(t)\n"
   ],
   "id": "7a3cedefccbe9d48",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  0  0  0  0]\n",
      " [ 0  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:58:55.721197Z",
     "start_time": "2025-02-26T08:58:55.655982Z"
    }
   },
   "cell_type": "code",
   "source": "t<10 # 返回小于10的索引",
   "id": "fefa001df105b4b9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True,  True,  True],\n",
       "       [ True,  True,  True,  True, False, False],\n",
       "       [False, False, False, False, False, False],\n",
       "       [False, False, False, False, False, False]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:59:10.845051Z",
     "start_time": "2025-02-26T08:59:10.836548Z"
    }
   },
   "cell_type": "code",
   "source": "t[t < 10] # 返回array([0, 0, 0, 0, 0, 0, 0, 7, 8, 9])",
   "id": "ec47df5968ed4e6f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 7, 8, 9])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T08:59:51.603598Z",
     "start_time": "2025-02-26T08:59:51.596110Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "t=t.clip(10,18) # clip截断10-18之间的数值，截断后返回\n",
    "print(t)"
   ],
   "id": "f659b008e5399998",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:01:03.206585Z",
     "start_time": "2025-02-26T09:01:03.201409Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# # 拓 展\n",
    "# # 三目运算（ np.where(condition, x, y)满足条件(condition)，输出x，不满足输出y。)）\n",
    "score = np.array( [[80, 88], [82, 81], [75, 81]] )\n",
    "result = np.where( score > 80, True, False ) # 80以上为True，否则为False\n",
    "print( result )"
   ],
   "id": "a0914aab5303b97b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 9 数组的添加，删除，去重",
   "id": "4de058d78464d5fb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:01:28.754976Z",
     "start_time": "2025-02-26T09:01:28.745967Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print('第一个数组：')\n",
    "print(a) #[[1 2 3] [4 5 6]]\n",
    "print('\\n')\n",
    "\n",
    "print(' 向 数 组 添 加 元 素 ：')\n",
    "print(np.append(a, [7, 8, 9])) # [1 2 3 4 5 6 7 8 9]\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴 0 添加元素：')\n",
    "print(np.append(a, [[7, 8, 9]], axis=0)) #轴是谁，谁就变,这里是0，所以是向下添加，[[1 2 3] [4 5 6] [7 8 9]]\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴 1 添加元素：')\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1)) # 这里是1，所以是向右添加，[[1 2 3 5 5 5] [4 5 6 7 8 9]]"
   ],
   "id": "11d68a94a9f53aa8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "\n",
      " 向 数 组 添 加 元 素 ：\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "\n",
      "沿轴 0 添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "\n",
      "\n",
      "沿轴 1 添加元素：\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:01:45.002749Z",
     "start_time": "2025-02-26T09:01:44.991922Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. numpy.insert\n",
    "# 函数在给定索引之前，沿给定轴在输入数组中插入值。# 如果值的类型转换为要插入，则它与输入数组不同。\n",
    "# 插入没有原地的，函数会返回一个新数组。 此外，如果未提供轴，则输入数组会被展开。\n",
    "\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a) # [[1 2] [3 4] [5 6]]\n",
    "print('\\n')\n",
    "\n",
    "print('未传递 Axis 参数。 在插入之前输入数组会被展开。')\n",
    "print(np.insert(a, 3, [11, 12])) # 在索引 3 之前插入 [11 12]\n",
    "print('\\n')\n",
    "print('传递了 Axis 参数。 会广播值数组来配输入数组。')\n",
    "\n",
    "print('沿轴  0 广播：')\n",
    "print(np.insert(a, 1, [11,12], axis=0)) # 在索引 1 之前插入 [11 12]\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴  1 广播：')\n",
    "print(np.insert(a, 1, 11, axis=1)) # 在索引 1 之前插入 11"
   ],
   "id": "7ab2665f7f3fb996",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在插入之前输入数组会被展开。\n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "\n",
      "\n",
      "传递了 Axis 参数。 会广播值数组来配输入数组。\n",
      "沿轴  0 广播：\n",
      "[[ 1  2]\n",
      " [11 12]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "\n",
      "沿轴  1 广播：\n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:01:57.292206Z",
     "start_time": "2025-02-26T09:01:57.282855Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(12).reshape(3,4)\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a) # [[ 0  1  2  3]\n",
    "            #  [ 4  5  6  7]\n",
    "            #  [ 8  9 10 11]]\n",
    "print('\\n')\n",
    "\n",
    "print('未传递 Axis 参数。 在删除之前输入数组会被展开。')\n",
    "print(np.delete(a,5)) # 删除第5行\n",
    "print('\\n')\n",
    "\n",
    "print('删除每一行中的第二列：')\n",
    "print(np.delete(a,1,axis = 1)) # 删除每一行中的第二列\n",
    "print('\\n')"
   ],
   "id": "b285181a65854235",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在删除之前输入数组会被展开。\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "\n",
      "\n",
      "删除每一行中的第二列：\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:02:05.512916Z",
     "start_time": "2025-02-26T09:02:05.501150Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([5,2,6,2,7,5,6,8,2,9])\n",
    "\n",
    "print ('第一个数组：')\n",
    "print (a) # [5 2 6 2 7 5 6 8 2 9]\n",
    "print ('\\n')\n",
    "\n",
    "print ('第一个数组的去重值：')\n",
    "u = np.unique(a)\n",
    "print (u) # [2 5 6 7 8 9]\n",
    "print ('\\n')\n"
   ],
   "id": "b41c3d5651797668",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "\n",
      "\n",
      "第一个数组的去重值：\n",
      "[2 5 6 7 8 9]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:02:15.630088Z",
     "start_time": "2025-02-26T09:02:15.610579Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print ('去重数组的索引数组：')\n",
    "u,indices = np.unique(a, return_index = True) # return_index=True表示返回索引,unique()函数会自动排序\n",
    "print (indices) # [1 0 2 4 7 9]\n",
    "print ('\\n')\n",
    "\n",
    "print ('我们可以看到每个和原数组下标对应的数值：')\n",
    "print (a) # [5 2 6 2 7 5 6 8 2 9]\n",
    "print ('去重数组的下标：')\n",
    "u,indices = np.unique(a,return_inverse = True)\n",
    "print (u) # [2 5 6 7 8 9]\n",
    "print (indices) # [1 0 2 0 3 1 2 4 0 5]\n",
    "print ('\\n')\n",
    "\n",
    "print ('返回去重元素的重复数量：')\n",
    "u,indices = np.unique(a,return_counts = True)\n",
    "print (u) # [2 5 6 7 8 9]\n",
    "print (indices) #[3 2 2 1 1 1]"
   ],
   "id": "abf6321206933f25",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "去重数组的索引数组：\n",
      "[1 0 2 4 7 9]\n",
      "\n",
      "\n",
      "我们可以看到每个和原数组下标对应的数值：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "去重数组的下标：\n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 0 3 1 2 4 0 5]\n",
      "\n",
      "\n",
      "返回去重元素的重复数量：\n",
      "[2 5 6 7 8 9]\n",
      "[3 2 2 1 1 1]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 10 数组的统计",
   "id": "4ad772c81b0c3af0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:02:25.139172Z",
     "start_time": "2025-02-26T09:02:25.131389Z"
    }
   },
   "cell_type": "code",
   "source": [
    "score = np.array([[80,88],[82,81],[75,81]])\n",
    "# 1. 获取所有数据最大值\n",
    "result = np.max(score)\n",
    "print(result) # 88\n",
    "# 2. 获取某一个轴上的数据最大值\n",
    "result = np.max(score,axis=0) #当前轴为0\n",
    "print(result) # [82 88]\n",
    "# 3. 获取最小值\n",
    "result = np.min(score,axis=1)\n",
    "print(result) # [75 81 80]\n",
    "# 4. 获取某一个轴上的数据最小值\n",
    "result = np.min(score,axis=0)\n",
    "print(result) # [75 81]\n"
   ],
   "id": "b939f6b1f1a4ee07",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88\n",
      "[82 88]\n",
      "[80 81 75]\n",
      "[75 81]\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:03:19.840433Z",
     "start_time": "2025-02-26T09:03:19.831410Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 5. 数据的比较\n",
    "result = np.maximum( [-2, -1, 0, 1, 2], 0 )  # 第一个参数中的每一个数与第二个参数比较返回大的\n",
    "print(result) # [0, 0, 0, 1, 2]\n",
    "result = np.minimum( [-2, -1, 0, 1, 2], 0 )  # 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "print(result) # [-2, -1, 0, 0, 0]\n",
    "result = np.maximum( [5, -1, 0, 1, 2], [1, 2, 3, 4, 5] )\n",
    "print(result) # [5, 2, 3, 4, 5]\n",
    "# 接受的两个参数，也可以大小一致;第二个参数只是一个单独的值时，其实是用到了维度的广播机制；\n",
    "\n",
    "# 6. 求平均值\n",
    "result = np.mean(score) # 获取所有数据的平均值\n",
    "print(result) # 81.166666666666667\n",
    "result = np.mean(score,axis=0) # 获取某一行或者某一列的平均值\n",
    "print(result) # [79 83.33333333]\n",
    "# 7. 求前缀和\n",
    "arr  = np.array([[1,2,3], [4,5,6]])\n",
    "print(arr) # [[1 2 3] [4 5 6]]\n",
    "\n",
    "'''\n",
    "[1, 2, 3]------>   |1 |2 |3 |\n",
    "[4, 5, 6]------>   |5=1+4 |7=2+5 |9=3+6|\n",
    "'''\n",
    "print(arr.cumsum(0)) # 按行求前缀和,打印结果为[[1 2 3] [5 7 9]]\n",
    "\n",
    "'''\n",
    "[1, 2, 3]------>   |1 |2+1   |3+2+1 |\n",
    "[4, 5, 6]------>   |4 |4+5   |4+5+6 |\n",
    "'''\n",
    "print(arr.cumsum(1)) # 按列求前缀和,打印结果为[[1 3 6] [4 9 15]]\n"
   ],
   "id": "aea82bccc019c64b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 1 2]\n",
      "[-2 -1  0  0  0]\n",
      "[5 2 3 4 5]\n",
      "81.16666666666667\n",
      "[79.         83.33333333]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]]\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 10.8. argmin求最小值索引",
   "id": "deb8d58e3e492097"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:04:18.016178Z",
     "start_time": "2025-02-26T09:04:18.007262Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "print(score) #打印原始数组\n",
    "result = np.argmin(score,axis=0)\n",
    "res=np.min(score,axis=0) #这样我们就可以知道最小的81是第二排的，是从前往后遍历的\n",
    "print(result,res) # [2 1] [75 81]"
   ],
   "id": "ea7b42625cf6680f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "[2 1] [75 81]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 10.9. 求每一列的标准差（这里是总体标准差）",
   "id": "3e80e87c5c696b9c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:04:57.829596Z",
     "start_time": "2025-02-26T09:04:57.819669Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 标准差是一组数据平均值分散程度的一种度量。一个较大的标准差，代表大部分数值和其平均值之间差异较大；\n",
    "# 一个较小的标准差，代表这些数据较接近平均值反应出数据的波动稳定情况，越大表示波动越大，越不稳定。\n",
    "result = np.std(score,axis=0)\n",
    "print(result) # [2.94364916 3.29983165]"
   ],
   "id": "933b2e9283f88925",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2.94392029 3.29983165]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 10.10 极 值",
   "id": "a03f12f910a83586"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:05:19.209055Z",
     "start_time": "2025-02-26T09:05:19.200996Z"
    }
   },
   "cell_type": "code",
   "source": [
    "result = np.ptp(score,axis=None)#就是最大值和最小值的差\n",
    "print(result) # 13"
   ],
   "id": "40770615f4aedb03",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 11 拼接与分割",
   "id": "97044462eb39ac4e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T09:23:43.150541Z",
     "start_time": "2025-02-26T09:23:43.111371Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1. 根据轴连接的数组序列\n",
    "a = np.array([[1,2],[3,4]])\n",
    "b = np.array([[5,6],[7,8]])\n",
    "print ('沿轴 0 连接两个数组：')\n",
    "print (np.concatenate((a,b),axis= 0).shape)\n",
    "print (np.concatenate((a,b),axis= 0))# concatenate(a,b)，按0轴拼接两个数组，变为4行2列\n",
    "print ('\\n')\n",
    "print ('沿轴 1 连接两个数组：')\n",
    "print (np.concatenate((a,b),axis = 1).shape) # concatenate(a,b)，按1轴拼接两个数组，变为2行4列\n",
    "print (np.concatenate((a,b),axis = 1))"
   ],
   "id": "a620936dd37d252b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 连接两个数组：\n",
      "(4, 2)\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "\n",
      "\n",
      "沿轴 1 连接两个数组：\n",
      "(2, 4)\n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 11.2 根据轴进行堆叠,会增加维度",
   "id": "19ae02401e3526b3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T10:30:18.239454Z",
     "start_time": "2025-02-26T10:30:18.227983Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a =np.arange(12).reshape(3,4) #3*4的数组\n",
    "b =np.arange(12).reshape(3,4)\n",
    "print(a)  #3*4的数组\n",
    "print('-'*50)\n",
    "print ('沿轴 0 连接两个数组：')\n",
    "print (np.stack((a,b),axis= 0).shape) # stack(a,b)，按0轴堆叠两个数组，变为2个上下叠放的3*4的数组\n",
    "print('-'*50)\n",
    "print (np.stack((a,b),axis= 0)) # 堆叠后，增加一个维度，变为2个3*4的数组\n",
    "print ('\\n')\n",
    "print ('沿轴 1 连接两个数组：')\n",
    "print (np.stack((a,b),axis = 1).shape) # stack(a,b)，按1轴堆叠两个数组，变为3个上下叠放的2*4的数组\n",
    "print('-'*50)\n",
    "print (np.stack((a,b),axis = 1))\n",
    "print ('沿轴 2 连接两个数组：')\n",
    "print (np.stack((a,b),axis = 2).shape) # stack(a,b)，按2轴堆叠两个数组，变为3个上下叠放的4*2的数组\n",
    "print('-'*50)\n",
    "print (np.stack((a,b),axis = 2))"
   ],
   "id": "8c174fe852420279",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "--------------------------------------------------\n",
      "沿轴 0 连接两个数组：\n",
      "(2, 3, 4)\n",
      "--------------------------------------------------\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]]\n",
      "\n",
      "\n",
      "沿轴 1 连接两个数组：\n",
      "(3, 2, 4)\n",
      "--------------------------------------------------\n",
      "[[[ 0  1  2  3]\n",
      "  [ 0  1  2  3]]\n",
      "\n",
      " [[ 4  5  6  7]\n",
      "  [ 4  5  6  7]]\n",
      "\n",
      " [[ 8  9 10 11]\n",
      "  [ 8  9 10 11]]]\n",
      "沿轴 2 连接两个数组：\n",
      "(3, 4, 2)\n",
      "--------------------------------------------------\n",
      "[[[ 0  0]\n",
      "  [ 1  1]\n",
      "  [ 2  2]\n",
      "  [ 3  3]]\n",
      "\n",
      " [[ 4  4]\n",
      "  [ 5  5]\n",
      "  [ 6  6]\n",
      "  [ 7  7]]\n",
      "\n",
      " [[ 8  8]\n",
      "  [ 9  9]\n",
      "  [10 10]\n",
      "  [11 11]]]\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 11.3. 矩阵垂直拼接（不重要）",
   "id": "3f36199b170874f9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T10:32:33.855114Z",
     "start_time": "2025-02-26T10:32:33.847021Z"
    }
   },
   "cell_type": "code",
   "source": [
    "v1 = [[0,1,2,3,4,5],\n",
    "   [6,7,8,9,10,11]]\n",
    "v2 = [[12,13,14,15,16,17],\n",
    "   [18,19,20,21,22,23]]\n",
    "result = np.vstack((v1,v2)) # vstack()函数，按垂直方向堆叠两个数组\n",
    "print(result)\n",
    "print('-'*50)\n",
    "print (np.stack((v1,v2),axis= 0).shape) # stack(a,b)，按0轴堆叠两个数组，变为2个上下叠放的3*4的数组\n",
    "print('-'*50)\n",
    "print (np.stack((v1,v2),axis= 0)) # 堆叠后，增加一个维度，变为2个3*4的数组\n",
    "\n",
    "# 和按0轴拼接的区别显而易见，vstack（）直接将两个数组拼为一个数组，而按0轴拼接在两个数组中间还是有一个很大的空白段"
   ],
   "id": "504a8b10aa83416a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(2, 2, 6)\n",
      "--------------------------------------------------\n",
      "[[[ 0  1  2  3  4  5]\n",
      "  [ 6  7  8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15 16 17]\n",
      "  [18 19 20 21 22 23]]]\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 11.4. 矩阵水平拼接",
   "id": "b044af633f68ba96"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T10:34:49.033636Z",
     "start_time": "2025-02-26T10:34:49.025207Z"
    }
   },
   "cell_type": "code",
   "source": [
    "v1 = [[0,1,2,3,4,5],\n",
    "   [6,7,8,9,10,11]]\n",
    "v2 = [[12,13,14,15,16,17],\n",
    "   [18,19,20,21,22,23]]\n",
    "result = np.hstack((v1,v2)) # hstack()函数，按水平方向堆叠两个数组\n",
    "print(result)"
   ],
   "id": "1e941755f363a855",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5 12 13 14 15 16 17]\n",
      " [ 6  7  8  9 10 11 18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 11.5 数组的分割",
   "id": "c26ef0a96ef13049"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T11:03:40.026906Z",
     "start_time": "2025-02-26T11:03:40.018145Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.arange(9).reshape(3,3)\n",
    "print(arr)\n",
    "print ('将数组分为三个大小相等的子数组：')\n",
    "b = np.split(arr,3,axis=0) # split(a,3)，按0轴将数组a分割为3个子数组\n",
    "#打印b分割后的数组形状\n",
    "print (b)\n"
   ],
   "id": "d5544ad58bbd4667",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "将数组分为三个大小相等的子数组：\n",
      "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]\n"
     ]
    }
   ],
   "execution_count": 58
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 11.6 垂直分割",
   "id": "95c684e4cf7a338d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T11:03:46.671686Z",
     "start_time": "2025-02-26T11:03:46.663767Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(16).reshape(4,4)\n",
    "\n",
    "print ('第一个数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "\n",
    "print ('垂直分割：')\n",
    "b = np.vsplit(a,2) # vsplit(a,2)，按垂直方向将数组4*4的数组分割为2个2*4子数组\n",
    "print (b)\n",
    "# 打印b分割后的数组形状\n",
    "for i in b:\n",
    "  print (i.shape)"
   ],
   "id": "6a714f148fec96f8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "\n",
      "\n",
      "垂直分割：\n",
      "[array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n",
      "(2, 4)\n",
      "(2, 4)\n"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T11:04:18.242709Z",
     "start_time": "2025-02-26T11:04:18.231887Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 水平分割\n",
    "a = np.arange(16).reshape(4,4)\n",
    "print ('第一个数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "print ('水平分割：')\n",
    "b = np.hsplit(a,2) # hsplit(a,2)，按水平方向将数组4*4的数组分割为2个4*2的子数组\n",
    "print (b)\n",
    "# 打印b分割后的数组形状\n",
    "for i in b:\n",
    "  print (i.shape)"
   ],
   "id": "e07bef4add7ac668",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "\n",
      "\n",
      "水平分割：\n",
      "[array([[ 0,  1],\n",
      "       [ 4,  5],\n",
      "       [ 8,  9],\n",
      "       [12, 13]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11],\n",
      "       [14, 15]])]\n",
      "(4, 2)\n",
      "(4, 2)\n"
     ]
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 12 nan和inf",
   "id": "edb8ef23b80e996c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T11:06:14.511415Z",
     "start_time": "2025-02-26T11:06:14.490737Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a,type(a)) #打印nan的类型 'float'\n",
    "print(b,type(b)) # 打印inf的类型 'float'"
   ],
   "id": "d0a3d3bb7ee532dd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n"
     ]
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T11:42:42.751258Z",
     "start_time": "2025-02-26T11:42:42.742225Z"
    }
   },
   "cell_type": "code",
   "source": "np.nan == np.nan # nan不等于nan，所以返回False",
   "id": "fa50e5fb403cbbce",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 72
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T11:07:33.980724Z",
     "start_time": "2025-02-26T11:07:33.970392Z"
    }
   },
   "cell_type": "code",
   "source": "np.inf > -np.inf # inf大于-inf，所以返回True",
   "id": "dd0c91ea0d8ba58",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T11:41:56.299371Z",
     "start_time": "2025-02-26T11:41:56.286833Z"
    }
   },
   "cell_type": "code",
   "source": [
    " # --判断数组中为nan的个数\n",
    "t = np.arange(24,dtype=float).reshape(4,6) # 生成24个数，reshape成4行6列的数组\n",
    "# 将三行四列的数改成nan\n",
    "t[2,4] = np.nan # 将t[2,4]的值改成nan\n",
    "print(t)\n",
    "t!=t"
   ],
   "id": "983b844dbbd39ecc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. nan 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False, False, False],\n",
       "       [False, False, False, False, False, False],\n",
       "       [False, False, False, False,  True, False],\n",
       "       [False, False, False, False, False, False]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 70
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T11:43:44.673516Z",
     "start_time": "2025-02-26T11:43:44.666600Z"
    }
   },
   "cell_type": "code",
   "source": "np.isnan(t) # 判断是否为nan",
   "id": "579ac88541b30b3f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False, False, False],\n",
       "       [False, False, False, False, False, False],\n",
       "       [False, False, False, False,  True, False],\n",
       "       [False, False, False, False, False, False]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 74
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T11:44:25.170865Z",
     "start_time": "2025-02-26T11:44:25.163553Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 可以使用np.count_nonzero() 来判断非零的个数\n",
    "# print(np.count_nonzero(t))\n",
    "# 并 且  np.nan != np.nan 的结果 是TRUE\n",
    "# 所以我们可以使用这两个结合使用判断nan的个数\n",
    "print(np.count_nonzero(t != t))\n",
    "\n",
    "# 将nan替换为0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)"
   ],
   "id": "e355e17d2c78bb44",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15.  0. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    }
   ],
   "execution_count": 75
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T12:16:51.579904Z",
     "start_time": "2025-02-26T12:16:51.476892Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4,6).astype('float')\n",
    "\n",
    "# 将数组中的一部分替换nan\n",
    "t[1,3:] = np.nan ## 将第二行从第3列开始到结束的值改成nan\n",
    "print(t)\n",
    "print('-------------------')\n",
    "# 遍历每一列，然后判断每一列是否有nan\n",
    "for i in range(t.shape[1]):\n",
    "   #获取当前列数据\n",
    "   temp_col = t[:,i] # 获取第i列的数据\n",
    "\n",
    "   # 判断当前列的数据中是否含有nan\n",
    "   nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "   # 条件成立说明含有nan\n",
    "   if nan_num != 0:\n",
    "      # 将这一列不为nan的数据拿出来\n",
    "      temp_col_not_nan = temp_col[temp_col == temp_col]\n",
    "\n",
    "      # 将数组中的 NaN 值替换为该数组中非 NaN 元素的均值\n",
    "      temp_col[np.isnan( temp_col )] = np.mean( temp_col_not_nan )\n",
    "\n",
    "print(t)"
   ],
   "id": "b7bad9f026dcaedd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "-------------------\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 14. 15.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    }
   ],
   "execution_count": 76
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 13转置与轴滚动",
   "id": "d8feb1753a35c9c3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T12:24:48.069452Z",
     "start_time": "2025-02-26T12:24:48.031485Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组：')\n",
    "print (a )\n",
    "print ('\\n')\n",
    "\n",
    "print ('对换数组：')\n",
    "print (np.transpose(a)) # transpose(a)，对换数组a的行和列\n",
    "\n",
    "# 与transpose一致\n",
    "a = np.arange(12).reshape(3,4)\n",
    "\n",
    "print ('原数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "\n",
    "print ('转置数组：')\n",
    "print (a.T)"
   ],
   "id": "b2afb3f3594e9b4b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 77
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T12:31:57.580063Z",
     "start_time": "2025-02-26T12:31:57.572083Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 转置轴\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "re = t1.swapaxes(1,0) # swapaxes(a,1,0)，交换数组a的1轴和0轴\n",
    "print (' 原 数 组 ：')\n",
    "print (t1)\n",
    "print ('\\n')\n",
    "\n",
    "print ('调用 swap axes 函数后的数组：')\n",
    "print (re)"
   ],
   "id": "7216c8705a8a8838",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "\n",
      "调用 swap axes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T12:28:46.426934Z",
     "start_time": "2025-02-26T12:28:46.412438Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 轴滚动\n",
    "t=np.ones((3,4,5,6)) #全1数组\n",
    "\n",
    "t1=np.rollaxis(t,2,0) # rollaxis(a,2,0)，将数组a的2轴向0轴滚动\n",
    "print ('原数组：')\n",
    "print(t1.shape)\n",
    "print(t1)"
   ],
   "id": "68f4cf2d76a7bf9d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "(5, 3, 4, 6)\n",
      "[[[[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]\n",
      "\n",
      "  [[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]\n",
      "\n",
      "  [[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]]\n",
      "\n",
      "\n",
      " [[[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]\n",
      "\n",
      "  [[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]\n",
      "\n",
      "  [[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]]\n",
      "\n",
      "\n",
      " [[[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]\n",
      "\n",
      "  [[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]\n",
      "\n",
      "  [[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]]\n",
      "\n",
      "\n",
      " [[[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]\n",
      "\n",
      "  [[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]\n",
      "\n",
      "  [[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]]\n",
      "\n",
      "\n",
      " [[[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]\n",
      "\n",
      "  [[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]\n",
      "\n",
      "  [[1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]\n",
      "   [1. 1. 1. 1. 1. 1.]]]]\n"
     ]
    }
   ],
   "execution_count": 80
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
